package com.ycg.biquge.config;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import com.ycg.biquge.interceptor.TokenInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author YCG
 * @date 2024-04-30 19:49
 * @description 跨域拦截
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private TokenInterceptor tokenInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                /*
                "https://blog.hope-api.top/",
                        "http://blog.hope-api.top:80/",
                        "http://8.138.133.92",
                        "http://localhost:5173"
                 */
                .allowedOriginPatterns("http://localhost:*","https://*.hope-api.top")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "PATCH")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/bookcase/**")
                .addPathPatterns("/user/logout")
                .excludePathPatterns("/user/login");
//        registry.addInterceptor(ipInterceptor).addPathPatterns("/**");
    }

}
